# -*- coding: utf-8 -*-
from zope.contentprovider.provider import ContentProviderBase
from zope.interface import implements, alsoProvides
from zope.publisher.interfaces import IRequest

from pargo.viewlet.interfaces import IViewlet


class ViewletBase(ContentProviderBase):
    """
    The Viewlet.
    """
    implements(IViewlet)
    
    def __init__(self, context, request, view, manager):
        super(ViewletBase, self).__init__(context, request, view)
        self.manager = manager
        self.__updated = False
        
    def update(self):
        pass

    def render(self):
        raise NotImplementedError


class PageTemplateViewlet(ViewletBase):
    """
    Viewlet with template in file.
    """
    template = None
    
    def __init__(self, context, request, view, manager):
        super(PageTemplateViewlet, self).__init__(context, request, view, manager)
        
        ## request and response objects must imitate Zope3 object
        ## because z3c.pt can only work with such objects
        alsoProvides(self.request, IRequest)
        
        response = self.request.response
        response.getHeader = response.headers.__getitem__
        response.setHeader = response.headers.__setitem__
    
    def render(self):
        """
        Render template.
        """
        result = u''
        if self.template is not None:
            result = self.template()
        
        return result
